package com.caishi.lkx.course.service.impl;

import com.aliyun.dysmsapi20170525.models.SendBatchSmsResponse;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.caishi.lkx.common.MyProperties;
import com.caishi.lkx.common.context.Context;
import com.caishi.lkx.common.sms.AliyunSmsModel;
import com.caishi.lkx.common.util.SmsUtilService;
import com.caishi.lkx.course.mapper.LiveUserMapper;
import com.caishi.lkx.course.model.LiveUserModel;
import com.caishi.lkx.course.service.ILiveUserService;
import com.caishi.lkx.user.model.GroupModel;
import com.caishi.lkx.user.service.IGroupService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.FutureTask;


@Service
public class LiveUserServiceImpl extends ServiceImpl<LiveUserMapper, LiveUserModel> implements ILiveUserService {

    @Resource
    private SmsUtilService smsUtilService;
    @Resource
    private LiveUserMapper liveUserMapper;
    @Resource
    private IGroupService groupService;
    @Resource(name = "customContext")
    protected Context context;
    // 由于定时任务不依赖于用户登录，获取groupId只能强制指定
    @Autowired
    private MyProperties myProperties;


    //    @Scheduled(cron = "*/5 * * * * ?")
//    @KSchedule(beanName = "liveUserServiceImpl", desc = "直播短信定时任务", cron = "*/5 * * * * ?")
    @Scheduled(cron = "0 0 8 * * ? ")
    public void sendAppointSms() {
        List<LiveUserModel> appointData = liveUserMapper.getAppointData();
        // 发送预约短信
        GroupModel groupModel = groupService.getById(myProperties.getGroupId());


        try {
            FutureTask<SendBatchSmsResponse> sendBatchSmsResponseFutureTask = smsUtilService.sendLiveNotification(appointData.stream().map(liveUserModel -> {
                Map<String, Object> map = new HashMap<>();
                map.put("phoneNumber", liveUserModel.getMobile());
                map.put("name", liveUserModel.getNickName());
                map.put("time", liveUserModel.getLiveTime());
                return map;
            }).toList(), SmsUtilService.SmsCodeType.liveAppointNotification, AliyunSmsModel.builder().signName(groupModel.getSmsSignName()).groupId(String.valueOf(context.getGroupId())).accessKeyId(groupModel.getSmsAccessKeyId()).accessKeySecret(groupModel.getSmsAccessKeySecret()).endpoint(groupModel.getSmsEndpoint()).build());
            sendBatchSmsResponseFutureTask.run();
            if (sendBatchSmsResponseFutureTask.get() != null) {
                if ("OK".equals(sendBatchSmsResponseFutureTask.get().getBody().getCode())) {
                    // 更新预约状态
                    liveUserMapper.update(null, new UpdateWrapper<LiveUserModel>().in("id", appointData.stream().map(LiveUserModel::getId).toList())
                            .set("is_send", Boolean.TRUE));
                }
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
